getCookie=function(name){
    var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr!=null)return unescape(arr[2]); return "";
}
var token= getCookie("token");
$(function(){
    //	获取个人资料
    $.ajax({
        url:'http://ww.yc630.com/api/index.php/Buyer/User/userinfo',
        type:"post",
        data:{
            'token':token
        },
        dataType:"JSON",
        success:function(data){
            var avatar = data['data']['avatar'];
            if(avatar==''){
                avatar = '../img/Group%2019.png';
            }
            $("#headPic").attr('src',avatar)
        }
    })
    //获得商家信息
    $.ajax({
        type:"post",
        url:"http://ww.yc630.com/api/index.php/Buyer/Goods/sellerdetail",
        data:{
            'token':token
        },
        dataType:'json',
        success:function(data){
            $("#icon_img").attr('src',data.data.icon_image)
            $("#icon_name").html(data.data.name)
        }
    });
//    蒙版操作
    $(".add_attr").click(function(){
        $(".mask_add").show()
    })
    $("#qx").click(function(){
        $(".mask_add").hide()
    })
    $("#qx_update").click(function(){
        $(".mask_update").hide()
    })
    $(".cancel").click(function(){
        window.history.go(-1)
    })
//    获取编辑属性
    var url=window.location.href
    var ids=url.split("?")[1]
    if(ids==undefined){
//    添加保存
        $(".save_back").click(function(){
            var attr_tpl_name=$("#attr_tpl_name").val()
            var attr_tpl_remark=$("#attr_tpl_remark").val()
            var tr_num=$(".z_list_main").length
            var str=""
            for(var i=0;i<tr_num;i++){
                var attr_name=$(".z_list_main").eq(i).children().eq(1).html()
                var attr_value=$(".z_list_main").eq(i).children().eq(2).html()
                var attr_nu=$(".z_list_main").eq(i).children().eq(3).html()
                str +=attr_name+":"+attr_value+":"+attr_nu+";"
            }
            console.log(attr_tpl_name)
            console.log(attr_tpl_remark)
            console.log(str)
            if(attr_tpl_name==""||attr_tpl_remark==""||str==""){
                alert('请填写信息')
            }
            else {
                $.ajax({
                    type:"post",
                    url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_add",
                    data:{
                        "tpl_name":attr_tpl_name,
                        "remark":attr_tpl_remark,
                        "attr":str,
                        "token":token
                    },
                    dataType:'json',
                    success:function(phpdata){
                        window.location.href="attr_tpl.html"
                    }
                })
            }
        })
    }else {
        var id=ids.substring(3)
        $.ajax({
            type:"post",
            url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_detail",
            data:{
                "id":id,
                "token":token
            },
            dataType:'json',
            success:function(phpdata){
                var tpl_name=phpdata['data'][id]['tpl_name']
                var remark=phpdata['data'][id]['remark']
                $("#attr_tpl_name").val(tpl_name)
                $("#attr_tpl_remark").val(remark)
                var str = '<tr class="z_head"><td>序号</td><td>属性名称</td><td>属性值列表</td><td>单位</td><td>操作</td></tr>';
                for(i in phpdata['data'][id]['attr']){
                    str +='<tr class="z_list_main"><td>'+i+'</td><td>'+phpdata['data'][id]['attr'][i]['attr_name']+'</td><td>'+phpdata['data'][id]['attr'][i]['value']+'</td><td>'+phpdata['data'][id]['attr'][i]['unit']+'</td><td><a onclick="bj(this)">编辑</a> <a onclick="delte(this)">删除</a></td></tr>'
                }
                $(".attr_list").html(str)
            }
        })
        //    编辑保存
        $(".save_back").click(function(){
            var attr_tpl_name=$("#attr_tpl_name").val()
            var attr_tpl_remark=$("#attr_tpl_remark").val()
            var tr_num=$(".z_list_main").length
            var str=""
            for(var i=0;i<tr_num;i++){
                var attr_name=$(".z_list_main").eq(i).children().eq(1).html()
                var attr_value=$(".z_list_main").eq(i).children().eq(2).html()
                var attr_nu=$(".z_list_main").eq(i).children().eq(3).html()
                str +=attr_name+":"+attr_value+":"+attr_nu+";"
            }
                $.ajax({
                    type:"post",
                    url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_update",
                    data:{
                        "id":id,
                        "tpl_name":attr_tpl_name,
                        "remark":attr_tpl_remark,
                        "attr":str,
                        "token":token
                    },
                    dataType:'json',
                    success:function(phpdata){
                        window.location.href="attr_tpl.html"
                    }
                })
        })
    }
//    添加单个属性
    $("#qd").click(function(){
        var attr_name=$("#attr_name").val()
        var attr_nu=$("#attr_nu").val()
        var attr_value=$("#attr_value").val()
        //var xh=parseInt($(".attr_list tr:last").children().eq(0).html())
        var xh=parseInt($(".attr_list tr").length)
        var aa=xh-1
        var str='<tr class="z_list_main"><td>'+aa+'</td><td>'+attr_name+'</td><td>'+attr_value+'</td><td>'+attr_nu+'</td><td><a onclick="bj(this)">编辑</a> <a onclick="delte(this)">删除</a></td></tr>'
        $(".attr_list").append(str)
        $(".mask_add").hide()
        $("#attr_name").val("")
        $("#attr_nu").val("")
        $("#attr_value").val("")
    })
//    编辑确定
    $("#qd_update").click(function(){
        var attr_name=$("#attr_name_update").val()
        var attr_nu=  $("#attr_nu_update").val()
        var attr_value=$("#attr_value_update").val()
        var a=parseInt($("#bjid").val())-1
        $(".z_list_main").eq(a).children().eq(1).html(attr_name)
        $(".z_list_main").eq(a).children().eq(2).html(attr_value)
        $(".z_list_main").eq(a).children().eq(3).html(attr_nu)
        $(".mask_update").hide()
    })
})
//    编辑单个属性
function bj(obj){
    var attr_name=$(obj).parent().parent().children().eq(1).html()
    var attr_nu=$(obj).parent().parent().children().eq(3).html()
    var attr_value=$(obj).parent().parent().children().eq(2).html()
    var aa=$(obj).parent().parent().index()
    $("#bjid").val(aa)
    $(".mask_update").show()
    $("#attr_name_update").val(attr_name)
    $("#attr_value_update").val(attr_value)
    $("#attr_nu_update").val(attr_nu)
}
//删除
function delte(obj){
    $($(obj).parent().parent().nextAll("tr")).each(function(){
        var a = $(this).find("td").eq(0).text();
        $(this).find("td").eq(0).text(a - 1);
    })
    $(obj).parent().parent().remove()
}